package com.wwfish.cmsui.modules.sysuser.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.nexustar.gwt.dashboard.client.AbstractPage;
import com.nexustar.gwt.dashboard.client.PageClient;
import com.nexustar.gwt.widgets.client.asyn.FishAsyncCallback;
import com.nexustar.gwt.widgets.client.asyn.IAsyncModelCallback;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.nexustar.gwt.widgets.client.model.PaginationModelDto;
import com.nexustar.gwt.widgets.client.ui.button.MyButton;
import com.nexustar.gwt.widgets.client.ui.form.FormContainer;
import com.nexustar.gwt.widgets.client.ui.form.FormElement;
import com.nexustar.gwt.widgets.client.ui.form.FormElementProviderAdpter;
import com.nexustar.gwt.widgets.client.ui.iprovider.IPaginationModelProvider;
import com.nexustar.gwt.widgets.client.ui.panel.ContentPanel;
import com.nexustar.gwt.widgets.client.ui.panel.InfoPanel;
import com.nexustar.gwt.widgets.client.ui.table.*;
import com.nexustar.gwt.widgets.client.ui.textbox.FishLabelTextBox;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolBar;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolItem;
import com.nexustar.gwt.widgets.client.ui.window.IMessageConfirmCall;
import com.nexustar.gwt.widgets.client.ui.window.Message;
import com.wwfish.cms.model.sysuser.SysUserDto;
import com.wwfish.cms.service.sysuser.SysUserManager;
import com.wwfish.cmsui.dashboard.client.CMSDashboard;
import com.wwfish.cmsui.modules.common.client.BaseCMSPage;
import com.wwfish.cmsui.modules.common.client.CMSPageClient;
import com.wwfish.cmsui.modules.common.client.util.ServiceFactory;
import com.wwfish.gwt.reflection.client.ModelReflection;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-23
 * Time: 15:01:52
 * To change this template use File | Settings | File Templates.
 */
public class SysUserManagePage extends BaseCMSPage {
    private PaginationTable tableList;
    private FormContainer searchForm;

    @Override
    protected boolean isRunAsync() {
        return true;
    }

    @Override
    public void initWidgets(final DockPanel page) {
        GWT.runAsync(new RunAsyncCallback() {
            public void onFailure(Throwable reason) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void onSuccess() {
                page.setWidth("100%");
                VerticalPanel vp = new VerticalPanel();
                vp.setWidth("100%");
                vp.setHeight("100%");
                vp.add(getMenuBar());
                initTableList();
                ContentPanel tableListWrapper = new ContentPanel("系统管理员列表");
                tableListWrapper.setWidth("100%");
                tableListWrapper.setHeight("100%");
                tableListWrapper.addContentWidget(tableList);
                vp.add(tableListWrapper);
                vp.setCellHeight(tableListWrapper, "96%");

                ContentPanel north = getSearchPanel();
                page.add(north, DockPanel.NORTH);
                page.setCellHeight(north, "45px");
                page.add(vp, DockPanel.CENTER);

                client.success(null);
            }
        });

    }

    private Widget getMenuBar() {
        ToolBar bar = new ToolBar();
        bar.setWidth("100%");
        ToolItem add = new ToolItem("添加");
        ToolItem edit = new ToolItem("编辑");

        ToolItem delete = new ToolItem("删除");
        bar.addToolItem(add);
        bar.addToolItem(edit);
        bar.addToolItem(delete);

        add.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                CMSDashboard.dispatchPage(SysUserPage.class.getName(), new CMSPageClient() {
                    @Override
                    protected void process(BaseCMSPage page) {
                        page.getModelManger().renderModel(null);
                    }
                });
            }
        });
        edit.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (tableList.getTable().getSelectedItem() != null) {
                    if (tableList.getTable().getSelectedItem() != null) {
                        CMSDashboard.dispatchPage(SysUserPage.class.getName(), new CMSPageClient() {
                            @Override
                            protected void process(BaseCMSPage page) {
                                page.getModelManger().renderModel(ModelReflection.clone(tableList.getTable().getSelectedItem().getUserObject()));
                            }
                        });
                    }

                }

            }
        });
        delete.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (tableList.getTable().getSelectedItem() != null) {
                    Message.confirm("确认要删除?", "温馨提示", new IMessageConfirmCall() {
                        public void doExcute(boolean flag) {
                            if (flag) {
                                ServiceFactory.invoke(SysUserManager.class.getName(), "delete", new Object[]{tableList.getTable().getSelectedItem().getUserObject()},
                                        new LoadingAsyncCallback() {
                                            @Override
                                            protected void success(Object result) {
                                                InfoPanel.show("删除成功！");
                                               ddOutPageModel(null);
                                            }
                                        });
                            }
                        }
                    });
                }
            }
        });
        return bar;
    }

    private void initTableList() {
        ViewTable table = new ViewTable(getColsWaiter());
        tableList = new PaginationTable(table, 20);
        tableList.getTable().setHeight("400px");
        tableList.getModelManger().setProvider(new IPaginationModelProvider() {
            public void setInputData(int begin, int count, final IAsyncModelCallback asyn) {
                ServiceFactory.invoke(SysUserManager.class.getName(), "getPageEntities", new Object[]{begin, count, searchForm.getModelManger().getModel()},
                        new LoadingAsyncCallback() {
                            @Override
                            protected void success(Object result) {
                                tableList.getModelManger().renderModel(result);
                            }
                        });
            }
        });
    }

    private ContentPanel getSearchPanel() {
        ContentPanel searchPanel = new ContentPanel("查询条件");
        searchPanel.setWidth("100%");
        final FishLabelTextBox sName = new FishLabelTextBox("名字:");
        final MyButton go = new MyButton("查询");
        go.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                ddOutPageModel(null);
            }
        });
        searchForm = new FormContainer(2);
        searchForm.getModelManger().renderModel(new HashMap(1));
        searchForm.addElement(new FormElement(sName, new FormElementProviderAdpter() {
            public void getValue(Object object) {
                ((Map) object).put("name", sName.getValue());
            }

            public void setValue(Object object) {
                sName.setValue(((Map) object).get("name"));
            }
        }));

        searchForm.addWidget(go);
        searchPanel.addContentWidget(searchForm);

        return searchPanel;
    }

    private ColumnWaiter getColsWaiter() {

        ColumnExt co1 = new ColumnExt("系统管理员名称", 200, new IColumnRender() {
            public void renderColumn(CellItem cell) {
                SysUserDto dto = (SysUserDto) cell.getUserObject();
                cell.setText(dto.getName());
            }
        });

        ColumnExt co3 = new ColumnExt("登录名", 250, new IColumnRender() {
            public void renderColumn(CellItem cell) {
                SysUserDto dto = (SysUserDto) cell.getUserObject();
                cell.setText(dto.getLoginName());
            }
        });

//        ColumnExt co5 = new ColumnExt("描述", 100, new IColumnRender() {
//            public void renderColumn(CellItem cell) {
//                SysUserDto dto = (SysUserDto) cell.getUserObject();
//                cell.setText(dto.get());
//            }
//        });
        ColumnWaiter cw = new ColumnWaiter(new ColumnExt[]{co1, co3});
        return cw;
    }

    @Override
    protected void ddOutPageModel(Object o) {
        tableList.getModelManger().renderAsyncModel(new Object[]{1, 20});
    }

    void addTableItem(Object user) {
        ((PaginationModelDto) tableList.getModelManger().getModel()).addModelItem(user, 0);
        tableList.getModelManger().refresh();
    }

    void editTableItem(Object user, Object old) {
        ((PaginationModelDto) tableList.getModelManger().getModel()).modifyModelItem(user, old);
        tableList.getModelManger().refresh();
    }
}
